<?php 
class ContactController extends Core_Controller
{
	public function indexAction()
	{
	  $this->_view->title = "Liên hệ";
   	  $this->_view->activeClass = "contact";
   	  if(count($this->_http->getArvs())>0) throw new Exception("404 not found",404);
   	  
   	  $this->_view->input_fullname = "";
   	  $this->_view->input_email = "";
   	  $this->_view->input_title = "";
   	  $this->_view->input_content = "";
   	  
   	  //status : 0 chua post, 1: sai captcha, 2: loi ,3: thanh cong
      $this->_view->status = 0;
      
      if(isset($_POST['sentButton']))
      {
      	$security = new Core_Security();
      	$fn = $security->xss_clean(trim($_POST['fullname']));
      	$e  = $security->xss_clean(trim($_POST['email']));
      	$tt = $security->xss_clean(trim($_POST['title']));
      	$ct = $security->xss_clean(trim($_POST['content']));
      	$cc = $security->xss_clean(trim($_POST['captcha']));
      	
      	if($cc != $_SESSION['code'])
      	{
      		$this->_view->status = 1;
      	}else 
      	{
      		$modelContact = new Model_Contact();
            $ok = $modelContact->insertContact($fn,$e,$tt,$ct);
            if(! $ok)
                 $this->_view->status = 2;
            else
            {
                 $this->_view->status = 3;
                 $config = $this->_registry['config'];
                 if($config['email']['receiver_contact'])
                 {
                    	loadHelper('mailer');
                        $mail = mailer($config['smtp']['is_smtp'],$config['smtp']['smtp_auth'],$config['smtp']['smtp_secure'],
                                       $config['smtp']['host'],$config['smtp']['port'],$config['smtp']['username'],$config['smtp']['password']);
    
                         try {
                              $mail->AddAddress($config['email']['email_admin'], 'Admin Bkloud');
                              $mail->SetFrom($e,$fn);
                              $mail->Subject = 'Liên hệ :'.$tt;
                              $mail->Body = $ct;
                              $mail->Send();
                             } 
                        catch (Exception $e) 
                        {}
                 }
            }     
      	}
      	
       $this->_view->input_fullname = $fn;
   	   $this->_view->input_email = $e;
   	   $this->_view->input_title = $tt;
   	   $this->_view->input_content = $ct;
      }
    }
}